import pymysql


class DB:
    def __init__(self):
        self.conn = pymysql.connect(host="localhost", user="root", password="123456",
                                    database="yun", port=3306, charset="utf8",
                                    cursorclass=pymysql.cursors.DictCursor)
        self.cousor = self.conn.cursor()

    # 添加，修改， 删除
    def update(self, sql):
        self.cousor.execute(sql)
        self.conn.commit()
        return self.cousor.lastrowid

    # 查询单个
    def find(self, sql):
        self.cousor.execute(sql)
        res = self.cousor.fetchone()
        return res

    # 查询所有
    def find_all(self, sql):
        self.cousor.execute(sql)
        res = self.cousor.fetchall()
        self.conn.commit()
        
        return res

    # 关闭
    def close(self):
        self.cousor.close()
        self.conn.close()

    def add(self,sql):
        self.cousor.execute(sql)

    def commit(self):
        self.conn.commit()

    def rollback(self):
        self.conn.rollback()


db1 = DB()
# res = db1.find_all(" select * from dept" )
# print(res)